<?php 

Class appModel3 extends Model
{
	public function get_app()
	{
		$arr=array();
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='appid' OR op_key='appsecret'";
		$temp=$this->getAll($sql);
		$arr['appid']=$temp[0]['op_value'];
		$arr['appsecret']=$temp[1]['op_value'];
		return $arr;
	}
	public function is_member()
	{
	
		if ((!isset($_SESSION['user_id']) || intval($_SESSION['user_id']) <= 0) &&
		ACTION_NAME!= 'login' )
		{
			/* session 不存在，检查cookie */
			if (!empty($_COOKIE['QIAQIA']['user_id']) && !empty($_COOKIE['QIAQIA']['user_pwd']))
			{
				// 找到了cookie, 验证cookie信息
				$sql = 'SELECT id, phone, password' .
						' FROM ' .$this->table('users') .
						" WHERE id = '" . intval($_COOKIE['QIAQIA']['user_id']) . "'";
				$row = $this->getRow($sql);
	
				if (!$row)
				{
					// 没有找到这个记录
					cookie($_COOKIE['QIAQIA']['user_id'],   '');
					cookie($_COOKIE['QIAQIA']['userpwd'], '');
	
					return false;
				}
				else
				{
					// 检查密码是否正确
					if (md5($row['password']) == $_COOKIE['QIAQIA']['userpwd'])
					{
	
	
	
						session('user_id',$row['id']);
						session('admin_name',$row['phone']);
						return true;
					}
					else
					{
						cookie($_COOKIE['QIAQIA']['user_id'],   '');
						cookie($_COOKIE['QIAQIA']['userpwd'], '');
	
						return false;
					}
				}
			}
			else
			{
				return false;
			}
		}
		else
		{
			return true;
		}
			
	}
	public function get_menu(){
		$sql="SELECT id,name,type,code,redirect_url as url FROM ".$this->table('category')." WHERE is_show = 1 AND parent_id=0 ORDER BY ordering DESC";
		$arr=$this->getAll($sql);
		foreach($arr as $k=>$v)
		{
			$sql="SELECT id,name,type,is_extend_links,redirect_url,code FROM ".$this->table('category')." WHERE is_show = 1 AND parent_id=".$v['id']." ORDER BY ordering DESC";
			$arr[$k]['subchild']=$this->getAll($sql);
		}
		
		foreach($arr as $k=>$v)
		{
				$arr[$k]['name']=hy_dig_filter($v['name']);
				if($v['is_extend_links']==1)
				{
					$arr[$k]['url']=$v['redirect_url'];
				}
				else 
				{
					if(REWRITE==1)
					{
						switch($v['type'])
						{
							case 1:$arr[$k]['url']="/".$v['code']."/article/index.html";break;
							case 2:$arr[$k]['url']="/".$v['code']."/article/photo.html";break;
							case 3:$arr[$k]['url']="/".$v['code']."/article/page.html";break;
							
						}
					}
					else 
					{
					  switch($v['type'])
					  {
						case 1:$arr[$k]['url']="index.php?g=home&m=article&a=index&code=".$v['code'];break;
						case 2:$arr[$k]['url']="index.php?g=home&m=article&a=photo&code=".$v['code'];break;
						case 3:$arr[$k]['url']="index.php?g=home&m=article&a=page&code=".$v['code'];break;
					
					  }
					}
				
				}
		   	if($v['subchild'])
			{
				foreach($v['subchild'] as $kk=>$vv)
				{
					$arr[$k]['subchild'][$kk]['name']=hy_dig_filter($vv['name']);
					if($_GET['code']==$vv['code'])
					{
						$arr[$k]['current']=1;
					}
					if($vv['is_extend_links']==1)
					{
						$arr[$k]['subchild'][$kk]['url']=$vv['redirect_url'];
					}
					else
					{
						if(REWRITE==1)
						{
							switch($vv['type'])
							{
								case 1:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/article/index.html";break;
								case 2:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/photo/index.html";break;
								case 3:$arr[$k]['subchild'][$kk]['url']="/".$vv['code']."/article/page.html";break;
							}
						}
						else 
						{
						  switch($vv['type'])
						  {
							case 1:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=article&a=index&code=".$vv['code'];break;
							case 2:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=photo&a=index&code=".$vv['code'];break;
							case 3:$arr[$k]['subchild'][$kk]['url']="index.php?g=home&m=article&a=page&code=".$vv['code'];break;
						  }
						}
					
					}
				}
				
			}
		   	
		}
		
		return $arr;
	} 
	public function current_menu($id)
	{
		$arr=array();
		if($id!='')
		{
		   $sql="SELECT name,keywords,description FROM ".$this->table('category')." WHERE id=".$id;

		   $arr=$this->getRow($sql);
		   $arr['name']=hy_dig_filter($arr['name']);
		 
		}
		return $arr;
	}
	public function where_am_i()
	{
		
		$model=MODULE_NAME;
		$action=ACTION_NAME;
		$id=isset($_GET['id'])?intval($_GET['id']):'';
		$urhere="";
		if($model=="content"&&$action="index")
		{
                if($id)
                {
                	$sql="SELECT name,id FROM ".$this->table('category')." WHERE id = '".$id."'";
                	$temp=$this->getRow($sql);
                	$urhere="<a href='#'>".hy_dig_filter($temp['name'])."</a>";
                }
		}
		elseif ($model=="msg"&&$action="index")
		{
			$urhere="<a href='#'>联系我们</a>";
		}
		elseif($model=="article"&&$action="view")
		{
                if($id)
                {
                	$sql="SELECT title,id FROM ".$this->table('article')." WHERE id = '".$id."'";
                	$temp=$this->getRow($sql);
                	$urhere="<a href='#'>".hy_dig_filter($temp['title'])."</a>";
                }
		}
		elseif($model=="photo"&&$action="album")
		{
			if($id)
			{
				$sql="SELECT title,id FROM ".$this->table('albums')." WHERE id = '".$id."'";
				$temp=$this->getRow($sql);
				$urhere="<a href='#'>".hy_dig_filter($temp['title'])."</a>";
			}
		}
		elseif($model=="pro"&&$action="view")
		{
			if($id)
			{
				$sql="SELECT cat_id FROM ".$this->table('product')." WHERE id = '".$id."'";
				$cat_id=$this->getOne($sql);
				
				$sql="SELECT name FROM ".$this->table('category')." WHERE id = '".$cat_id."'";
				$parent=$this->getRow($sql);
				$urhere="<a href='index.php?g=home&m=content&a=index&id=".$cat_id."'>".hy_dig_filter($parent['name'])."</a>";
				
				$sql="SELECT title,id FROM ".$this->table('product')." WHERE id = '".$id."'";
				$temp=$this->getRow($sql);
				$urhere.=">><a href='#'>".hy_dig_filter($temp['title'])."</a>";
			}
		}
		
		
		return $urhere;
	}
	public function getPClass($id)
	{
		$sql="SELECT p.code,p.id FROM ".$this->table('category')." as p,".$this->table('category')." as c WHERE c.parent_id=p.id AND c.id='".$id."'";
		$res=$this->getRow($sql);
		
		return $res;
	}
	public function getCurClass($id)
	{
		$sql='SELECT code,id,name,thumb,type FROM '.$this->table('category')." WHERE id='".$id."'";
		$res=$this->getRow($sql);
		if($res)
		{
			$res['ch_name']=hy_dig_filter($res['name'],1);
			$res['en_name']=hy_dig_filter($res['name'],2);
				$res['name']=hy_dig_filter($res['name']);
				if(REWRITE==1)
				{
					switch($res['type'])
					{
						case 1:$parent['url']="/".$res['code']."/article/index.html";break;
					    case 2:$parent['url']="/".$res['code']."/article/photo.html";break;
					    case 3:$parent['url']="/".$res['code']."/article/page.html";break;
					}
				}
				else
				{
			    	switch($res['type'])
				  {
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$res['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$res['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$res['code'];break;
				
				  }
				}
				
			
		}
	
		return $res;
	}
	//获取分类下所有的子分类
	public function getCatTree($arrCat, $parent_id = 0, $level = 0)
	{
		static $arrTree =array(); 
		if($level==0)
		{
			$arrTree=array();
		}
	     $sql='SELECT id,parent_id FROM '.$this->table('category')." WHERE parent_id='".$parent_id."'";
	     $arrCat=$this->getAll($sql);
	     $level++;
	    if(empty($arrCat))
	    {
	    	return false;
	    }
	    foreach($arrCat as $key => $value)
	    {
	    	$arrTree[] = $value['id']; 
	    	$this->getCatTree($arrCat, $value['id'],$level);
	    }
		 return array_flip(array_flip($arrTree));
	}
	public function getSubClass($id)
	{
		$sql='SELECT code,id,name,thumb,type FROM '.$this->table('category')." WHERE parent_id='".$id."'";
		$res=$this->getAll($sql);
		if($res)
		{
		foreach($res as $k=>$v)
		{
			$res[$k]['name']=hy_dig_filter($v['name'],1);
			$res[$k]['en_name']=hy_dig_filter($v['name'],2);
			if(REWRITE==1)
			{
			     switch($v['type'])
				{
					    case 1:$parent['url']="/".$v['code']."/article/index.html";break;
					    case 2:$parent['url']="/".$v['code']."/article/photo.html";break;
					    case 3:$parent['url']="/".$v['code']."/article/page.html";break;
				}
			}
			else
			{
				
				switch($v['type'])
				{
					case 1:$parent['url']="index.php?g=home&m=article&a=index&code=".$v['code'];break;
					case 2:$parent['url']="index.php?g=home&m=article&a=photo&code=".$v['code'];break;
					case 3:$parent['url']="index.php?g=home&m=article&a=page&code=".$v['code'];break;
				}
			}
			
		}
		}
		
		return $res;
	}
    public function code_to_id($code)
    {
    	$sql='SELECT id FROM '.$this->table('category')." WHERE code='".$code."'";
    	return $this->getOne($sql);
    }
	public function get_ad($opt)
	{
		$option=array();
		$sql="SELECT id FROM ".$this->table('block')." WHERE code ='".$opt['code']."'";
		$flash_id=$this->getOne($sql);
		if($flash_id)
		{
		$sql="SELECT weight,height FROM ".$this->table('block')." WHERE id ='".$flash_id."'";
		$flash_cfg=$this->getRow($sql);
		
		$sql="SELECT * FROM ".$this->table('block_content')." WHERE block_id ='".$flash_id."' ORDER BY ordering DESC";
		$flash_img=$this->getAll($sql);
		
		
		  foreach($flash_img as $k=>$v)
		  {
		  	$flash_img[$k]['ch_title']=hy_dig_filter($v['title'],1);
		  	$flash_img[$k]['en_title']=hy_dig_filter($v['title'],2);
		  	$flash_img[$k]['title']=hy_dig_filter($v['title']);
		  }
		}
	     return $flash_img;
	}
	public function get_cfg($key='')
	{
		if($key)
		{
			$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='".$key."'";
			$arr=$this->getOne($sql);
			
		}
		else 
		{
		   $sql="SELECT * FROM ".$this->table('options')." ";
		   $arr=array();
		   $res=$this->getAll($sql);
		   
		   if(count($res)>0)
		  {
			  foreach($res as $k=>$v)
			  {
				$arr[$v['op_key']]=$v['op_value'];
			  }
		   }
		}
		
		return $arr;
	}
	
	public function get_count_openid($openid)
	{
		$sql = "select count(*) from ".$this->table('wxusers')." where openid='".$openid."'";
		return $this->getOne($sql);
	}
	
	public function insert_into_wxuser($info)
	{
		$data = yf_set_insert($info);
		$sql = "insert into ".$this->table('wxusers')." (".$data['key'].") values (".$data['val'].")";
		if($this->query($sql))
		{
			return $this->insert_id();
		}
		else
		{
			return false;
		}
	}
}